<template>
  <div>
    <Nav></Nav>
    <el-row :gutter="20">
      <el-col :span="22">
        <div class="formstyle">
          <el-form :label-position="labelPosition" :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="原密码" prop="expass">
              <el-input v-model="ruleForm.expass"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
              <el-input type="text" v-model="ruleForm.pass" autocomplete="new-password" onfocus="this.type='password'"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
              <el-input type="text" v-model="ruleForm.checkPass" autocomplete="new-password" onfocus="this.type='password'"></el-input>
            </el-form-item>
            <div class="btn">
              <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </div>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { updatePassword } from '@/api/api'
import nav from '../../components/navigation'
import { mapGetters } from 'vuex'
export default {
  components: {
    Nav: nav,
  },
  data () {
    var checkexpass = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('原密码不能为空'))
      }
      callback()
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      labelPosition: 'left',
      ruleForm: {
        pass: '',
        checkPass: '',
        expass: '',
      },
      rules: {
        pass: [{ validator: validatePass, trigger: 'blur' }],
        checkPass: [{ validator: validatePass2, trigger: 'blur' }],
        expass: [{ validator: checkexpass, trigger: 'blur' }],
      },
    }
  },
  computed: {
    ...mapGetters(['username'])
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) return false
        let params = {
          userName: this.username,
          oldPwd: this.ruleForm.expass,
          newPwd: this.ruleForm.pass
        }
        updatePassword(params).then(res => {
          if (res.code == 200) {
            swal('提示', res.message + '将重新登录' || '密码修改成功', 'success').then(() => {
              this.$router.replace({path: '/'})
            })
          } else {
            swal('错误', res.message || '修改失败', 'warning')
          }
        }).catch((err) => {
          swal('错误', '网络连接错误', 'error')
        })
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  },
}
</script>
<style lang="less" scoped>
.main {
  margin: 20px;
}
.formstyle {
  margin: 30px auto;
  height: 500px;
  width: 600px;
  // width: 60%;
}

.btn {
  width: 160px;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
}
</style>